<?php
namespace Home\Controller;
use Think\Controller;
use Home\Controller\Common;
class IndexController extends CommonController {

    public function index(){
    // $fuid=I('fuid');
    // $tuid=I('tuid');
    // $sdkappid='1400133121'; 
    // $private_key_path='c://private_key';
    // # 这里需要写绝对路径，开发者根据自己的路径进行调整
    // $command = 'c://signature.exe'
    // . ' ' . escapeshellarg($private_key_path)
    // . ' ' . escapeshellarg($sdkappid)
    // . ' ' . escapeshellarg($fuid);
    // $ret = exec($command, $out, $status);
    // if ($status == -1)
    // {
    //     return null;
    // }
    // // echo $out[0].'<br/>';
    // // echo $identifier;
    // // exit();
    //   $account_id=$tuid;
    //   $receiver=$fuid;
    //   $text_content='你好!需要什么帮助';
    //   $info=$this->TimRestApi->openim_send_msg($account_id, $receiver, $text_content);
    // $this->assign('userSig',$out[0]);
    // $this->assign('tuid',$tuid);
    // $this->assign('fuid',$fuid);
    $this->display();
    }
    
    //发送消息
   public function openim_send_msg(){
      $account_id=I('account_id');
      $receiver=I('receiver');
      $text_content=I('text_content');
      $info=$this->TimRestApi->openim_send_msg($account_id, $receiver, $text_content);
      dump($info);
    }


    //获取对话消息  
   public function receive(){
     // $body = '{"MsgBody":[{"MsgType":"TIMCustomElem","MsgContent":{"Desc":"","Data":"{\"type\":\"audio\",\"url\":\"https://test-1252230039.cos.ap-chengdu.myqcloud.com/0.39040302230540624.o6zAJs540fbwA73NjGVnrfDhQl08.5PmsNV7fgQc1dafc7ae8e0ba762303a7a36a294e07be.durationTime%3D2283.mp3\"}","Ext":""}}],"CallbackCommand":"C2C.CallbackAfterSendMsg","From_Account":"30","To_Account":"10","MsgRandom":1499057884,"MsgSeq":2819674,"MsgTime":1536427297}';
      $body = serialize(file_get_contents('php://input'));
      // $bodys=json_decode($body,true) ;
      // $iunfo= json_decode($bodys['MsgBody'][0]['MsgContent']['Data']);
      // $From_Account=json_decode($bodys['From_Account']);
      // $To_Account=json_decode($bodys['To_Account']);
    // echo 123;
    // $url  = file_get_contents('http://www.kuitao8.com/images/adindex03.jpg');
      // $info=file_put_contents('/images/image.jpg', $image);
      // dump($url);
    // $info=$this->TimRestApi->openim_send_msg_pic('30', '10', 'https://www.cwlyz.cn/Public/img/logo.png');
      // dump($iunfo->url);
      // $info=$bodys['MsgBody'][0]['MsgContent']['Data'];
      file_put_contents('./111.txt',$body);
   }
   //创建用户
   public function addUser(){
      $newtime=M('time')->field('time')->order('time desc')->find();
      $nowtime=time();
      $detime=$nowtime-$newtime['time'];
      if($detime>=60){
      $addwhere['time']=$nowtime;
      M('time')->add($addwhere);
      $id=M('txusers')->order('uid desc')->find();
      $star=$id['uid']+1;
      $addid=$id['uid']+100;
      for ($i=$star; $i <$addid ; $i++) { 
             $i=(string)$i;
             $addwhere['uid']=$i;
             $info=$this->TimRestApi->account_import($identifier=$i,$nick=$i,$face_url='http://www.cwlyz.cn/Public/img/logo.png');
             // dump($info);
             if($info['ActionStatus']=='OK'){
                M('txusers')->add($addwhere);
             }
      }
      }
 
      
   }

   //创建群
   public function group_create_group(){
     $info=$this->TimRestApi->group_create_group('Public','groptest','laosong');
     dump($info);
   }
  //添加好友
   public function sns_friend_import(){
     $info=$this->TimRestApi->sns_friend_import('def','30');
     dump($info);
   }
  //status
   public function seeuserid(){
    $usersigdeletenuminfo=M('usersigdeletenum')->order('num desc')->find();
    $num=$usersigdeletenuminfo['num']*3000;
    for($i=$num;$i<=($num+3000);$i++){
          $where['uid']=$i;
          $txusers=M('txusers')->where($where)->find();
          if(!$txusers){
              $addwhre['uid']=$i;
              $addwhre['type']=1;
              M('usersigdelete')->add($addwhre); 
          }
    }
              $wherenum['num']=$usersigdeletenuminfo['num']+1;
              M('usersigdeletenum')->add($wherenum);
   }

   //创建用户sig 防止以后腾讯IM收费
    public function getUserSig( $sdkappid='1400133121', $private_key_path='c://private_key')
{   
      $newtime=M('usersigtime')->order('usersigtime desc')->find();
      $nowtime=time();
      $detime=$nowtime-$newtime['usersigtime'];
      if($detime>=60){
      $addwhere['usersigtime']=$nowtime;
      M('usersigtime')->add($addwhere);
      $txusersinfo=M('txusers')->limit($newtime['id']*100,100)->select();
      foreach ($txusersinfo as $key => $value) {
          $out='';
          $identifier=$value['uid'];
          # 这里需要写绝对路径，开发者根据自己的路径进行调整
          $command = 'c://signature.exe'
          . ' ' . escapeshellarg($private_key_path)
          . ' ' . escapeshellarg($sdkappid)
          . ' ' . escapeshellarg($identifier);
          $ret = exec($command, $out, $status);
          if ($status == -1)
          {
              return null;
          }
         
          $addwhereusersig['usig']= $out[0];
          $addwhereusersig['uid']= $identifier;
          M('usersig')->add($addwhereusersig);
      }
      } 
}

  public function getUserSigtest(    $sdkappid='14001331211', $private_key_path='/www/wwwroot/im_wyscdz_com/ThinkPHP/Library/Vendor/PhpImServerSdk/signature/private_key'){
           $identifier=1;
          # 这里需要写绝对路径，开发者根据自己的路径进行调整
           $signature='/www/wwwroot/im_wyscdz_com/ThinkPHP/Library/Vendor/PhpImServerSdk/signature/signature';
          $command = $signature
          . ' ' . escapeshellarg($private_key_path)
          . ' ' . escapeshellarg($sdkappid)
          . ' ' . escapeshellarg($identifier);
          $ret = exec($command, $out, $status);
          dump($out[0]);
  }





    public function adminGetUserSig( $sdkappid='1400133121', $private_key_path='c://private_key')
{ 
    
    $identifier='sysadmin';
    # 这里需要写绝对路径，开发者根据自己的路径进行调整
    $command = 'c://signature.exe'
    . ' ' . escapeshellarg($private_key_path)
    . ' ' . escapeshellarg($sdkappid)
    . ' ' . escapeshellarg($identifier);
    $ret = exec($command, $out, $status);
    if ($status == -1)
    {
        return null;
    }
    echo $out[0];
}
}